// 格式化时间戳
function add0(m) {
    return m < 10 ? '0' + m : m
}

//时间戳转换
const timeFormat = (timestamp) => {
    //timestamp是整数，否则要parseInt转换,不会出现少个0的情况
    var time = new Date(timestamp);
    var year = time.getFullYear();
    var month = time.getMonth() + 1;
    var date = time.getDate();
    var hours = time.getHours();
    var minutes = time.getMinutes();
    return year + '-' + add0(month) + '-' + add0(date) + ' ' + add0(hours) + ':' + add0(minutes)
}

/*
 *获取今日的起始和结束时间
 *返回值："起始时间,结束时间"
 */
function ToDayStr() {
    var returnStr = "";
    var date = new Date(); //当前时间
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    //var hour = date.getHours();
    //var minutes = date.getMinutes();
    //var second = date.getSeconds();
    month = month < 10 ? "0" + month : month;
    day = day < 10 ? "0" + day : day;
    returnStr = year + "/" + month + "/" + day + " 00:00:00,"; //起始时间
    returnStr += year + "/" + month + "/" + day + " 23:59:59"; //结束时间
    return returnStr;
}

/*获取本周的起始和结束时间
 *返回值："起始时间,结束时间"
 */
function ThisWeekStr() {
    var returnStr = "";
    var date = new Date(); //当前时间
    var week = date.getDay(); //获取今天星期几
    var monday = GetDate2(week - 1, 1, date); //获取星期一
    var sunday = GetDate2(7 - week, 2, date); //获取星期天
    //起始时间的年月日
    var year1 = monday.getFullYear();
    var month1 = monday.getMonth() + 1;
    var day1 = monday.getDate();
    //结束时间的年月日
    var year2 = sunday.getFullYear();
    var month2 = sunday.getMonth() + 1;
    var day2 = sunday.getDate();
    //处理起始时间小于10的追加"0"在前面
    month1 = month1 < 10 ? "0" + month1 : month1;
    day1 = day1 < 10 ? "0" + day1 : day1;
    //处理结束时间小于10的追加"0"在前面
    month2 = month2 < 10 ? "0" + month2 : month2;
    day2 = day2 < 10 ? "0" + day2 : day2;

    returnStr = year1 + "/" + month1 + "/" + day1 + " 00:00:00,"; //起始时间
    returnStr += year2 + "/" + month2 + "/" + day2 + " 23:59:59"; //结束时间
    return returnStr;
}

/*
 *获取本月的起始和结束时间
 *返回值："起始时间,结束时间"
 */
function ThisMonthStr() {
    var returnStr = "";
    var date = new Date(); //当前时间
    var year = date.getFullYear();
    var month = date.getMonth();

    var min = new Date(year, month, 1); //本月月初
    var max = new Date(year, month + 1, 0); //本月月底

    //起始时间的年月日
    var year1 = min.getFullYear();
    var month1 = min.getMonth() + 1;
    var day1 = min.getDate();
    //结束时间的年月日
    var year2 = max.getFullYear();
    var month2 = max.getMonth() + 1;
    var day2 = max.getDate();
    //处理起始时间小于10的追加"0"在前面
    month1 = month1 < 10 ? "0" + month1 : month1;
    day1 = day1 < 10 ? "0" + day1 : day1;
    //处理结束时间小于10的追加"0"在前面
    month2 = month2 < 10 ? "0" + month2 : month2;
    day2 = day2 < 10 ? "0" + day2 : day2;

    returnStr = year1 + "/" + month1 + "/" + day1 + " 00:00:00,"; //起始时间
    returnStr += year2 + "/" + month2 + "/" + day2 + " 23:59:59"; //结束时间
    return returnStr;
}
/*
 *获取传入的日期前N天或后N日期(N = day)
 *type:1：前；2：后
 *date：传入的日期
 */
function GetDate2(day, type, date) {
    var zdate;
    if (date === null || date === undefined) {
        zdate = new Date();
    } else {
        zdate = date;
    }
    var edate;
    if (type === 1) {
        edate = new Date(zdate.getTime() - (day * 24 * 60 * 60 * 1000));
    } else {
        edate = new Date(zdate.getTime() + (day * 24 * 60 * 60 * 1000));
    }
    return edate;
}

export {
    timeFormat,
    ToDayStr,
    ThisWeekStr,
    ThisMonthStr
}
